package com.tadpole.music.view.fragment.study;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.study.NowStudyAdapter;
import com.tadpole.music.app.MyApplication;
import com.tadpole.music.bean.me.VipListBean;
import com.tadpole.music.bean.study.StudyBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.me.VipListIView;
import com.tadpole.music.presenter.me.VipListPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.question.SimulationPracticeActivity;
import com.tadpole.music.view.activity.study.AnswerStatisticsActivity;
import com.tadpole.music.view.activity.study.MyErrorTopicActivity;
import com.tadpole.music.view.activity.study.MyExamsActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 学习
 * Created By 孙岩
 * on 2020/9/8
 */
public class StudyFragment extends BaseFragment implements VipListIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    //我的考试
    private LinearLayout llMyExam;
    //我的错题
    private LinearLayout llWrongTopic;
    //答题统计
    private LinearLayout llStatistics;
    //我的课程
    private LinearLayout llMyCurriculum;

    private TextView tvTime;
    //最近学习recyclerView
    private RecyclerView recyclerView;
    //最近学习adapter
    private NowStudyAdapter nowStudyAdapter;

    //presenter
    private VipListPresenter vipListPresenter;
    //封装的数据
    private List<StudyBean> studyBeanList = new ArrayList<>();

    @Override
    public int getContentViewId() {
        return R.layout.fragment_study;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initView(View mRootView) {
        llMyExam = mRootView.findViewById(R.id.llMyExam);
        llWrongTopic = mRootView.findViewById(R.id.llWrongTopic);
        llStatistics = mRootView.findViewById(R.id.llStatistics);
        llMyCurriculum = mRootView.findViewById(R.id.llMyCurriculum);
        recyclerView = mRootView.findViewById(R.id.recyclerView);
        tvTime = mRootView.findViewById(R.id.tvTime);

        initListeners();

        vipListPresenter = new VipListPresenter();
        vipListPresenter.attachView(this);
        vipListPresenter.getVipList("2","0","0",false);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(SpUtil.getInstance(getActivity()).getString(Constant.TOKEN,"").equals("")){
            tvTime.setText("0");
        }else{
            tvTime.setText(Mutils.formatTime(System.currentTimeMillis()-MyApplication.time));
        }
    }

    public static StudyFragment newInstance(String param1, String param2) {
        StudyFragment fragment = new StudyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    //监听器
    private void initListeners() {
        //我的错题
        llWrongTopic.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), MyErrorTopicActivity.class);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //答题统计
        llStatistics.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), AnswerStatisticsActivity.class);
                        startActivity(intent);
//                        ToastUtils.show("敬请期待");
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //我的考试
        llMyExam.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), MyExamsActivity.class);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //我的课程
        llMyCurriculum.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        ToastUtils.show("敬请期待");
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void show401() {

    }

    @Override
    public void showVipList(List<VipListBean.DataBean> list) {
        for(int i=0;i<list.size();i++){
            if(list.get(i).isPaid()){
                StudyBean studyBean = new StudyBean(
                        list.get(i).getName(),
                        list.get(i).getThumb(),
                        list.get(i).getCollege_id(),
                        list.get(i).getCollege().getName(),
                        list.get(i).getRank().getId()
                );
                studyBeanList.add(studyBean);
            }
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        nowStudyAdapter = new NowStudyAdapter(getActivity(), studyBeanList);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(nowStudyAdapter);

        nowStudyAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if(Mutils.isNetworkAvailable()){
                    if(SpUtil.getInstance(getActivity()).getString(Constant.TOKEN,"").equals("")){
                        Intent intent = new Intent(getActivity(),LoginActivity.class);
                        startActivity(intent);
                    }else{
                        Intent intent = new Intent(getActivity(), SimulationPracticeActivity.class);
                        intent.putExtra("id", studyBeanList.get(position).getId());
                        intent.putExtra("name", studyBeanList.get(position).getuName());
                        intent.putExtra("rank_id",studyBeanList.get(position).getRank_id());
                        startActivity(intent);
                    }
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void stopRefresh() {

    }
}
